package com.edu.chapter09_kotlin

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity(), View.OnClickListener {
    // 延迟初始化，只能用var
    private lateinit var textView: TextView
    private val j: Double? = null
    private var i: Double = 10.0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById(R.id.tv_hello)
        textView.setOnClickListener(this)
//        i = j
        if (j != null) {
            i = j
        }

        val arrayInt = arrayOf(1, 2, 2, 3)
        val arrayString = arrayOf("apple")
        print(arrayInt.javaClass)
        println(getLength("Hello world"))
    }

    override fun onClick(v: View?) {
        if (v != null) {
            if (v.id == R.id.tv_hello) {
                Toast.makeText(this, "点了一下", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private fun getLength(text: String?): Int {
        return text?.length ?: 0
    }

}
